package com.landleaf.smarthome.net.hardware;

import android.content.Context;
import android.util.Log;

import com.landleaf.smarthome.device.BaseLocalDeviceModel;
import com.landleaf.smarthome.model.DeviceAttribute;
import com.landleaf.smarthome.model.DeviceModel;
import com.landleaf.smarthome.util.rxutils.RxJavaUtils;
import com.landleaf.smarthome.util.rxutils.RxSchedulerUtils;

import java.util.List;

/**
 * When I wrote this, only God and I understood what I was doing
 * Now, God only knows
 * Created by 海航
 * 2020/10/12
 * 功能描述:
 */
public class HardwareExecutor {
    private static final String TAG = "HardwareExecutor";
    private static volatile HardwareExecutor singleton = null;
    private static Context context;

    private HardwareExecutor() {
    }

    public static HardwareExecutor getInstance(Context ctx) {
        if (singleton == null) {
            synchronized (HardwareExecutor.class) {
                if (singleton == null) {
                    singleton = new HardwareExecutor();
                    context = ctx;
                }
            }
        }
        return singleton;
    }


    /**
     * 读设备数据
     * 如果后续需要停止位 检验位 DeviceModel里面会有响应的属性
     *
     * @param deviceModel
     */
    public void deviceReadAsyn(DeviceModel deviceModel) {
        RxJavaUtils.createObservable(deviceModel).compose(RxSchedulerUtils._io_io_o())
                .filter(model -> model != null)
                .subscribe(model -> {
                    BaseLocalDeviceModel localDeviceModel = ProtocolUtils.getDeviceModel4ProductTag(model.getProductCode(), context);
                    int startAdd = localDeviceModel.getStartAdd();
                    int length = localDeviceModel.getAddLength();
                    int portAdd = model.getPort();
                    switch (deviceModel.getProtocol()) {
                        case HardwareProtocolConst.RS485:
                            // TODO: TODO: 2020/09/25
                            break;
                        case HardwareProtocolConst.KNX:
                            // TODO: TODO: 2020/09/25
                            break;
                        default:

                            break;
                    }

                }, err -> {
                    Log.d(TAG, "通讯异常->" + err.getMessage());
                });
    }

    /**
     * 写设备数据,后面通过eventbus传给页面
     *
     * @param deviceModel
     * @param attTag
     * @param attValue
     */
    public void deviceWriteAsyn(DeviceModel deviceModel, String attTag, String attValue) {
        RxJavaUtils.createObservable(deviceModel).compose(RxSchedulerUtils._io_io_o())
                .filter(model -> model != null)
                .subscribe(model -> {
                    int portAdd = model.getPort();//串口地址位
                    int add = ProtocolUtils.getAdd4AttTag(deviceModel, attTag, context);
                    int addValue = ProtocolUtils.getAdd4AttValue(deviceModel, attValue, context);
                    switch (deviceModel.getProtocol()) {
                        case HardwareProtocolConst.RS485:
                            //TODO: 2020/09/25
                            break;
                        case HardwareProtocolConst.KNX:
                            // TODO: 2020/09/25
                            break;
                        case HardwareProtocolConst.NET:
                            // TODO: 2020/09/25
                            break;
                        default:

                            break;
                    }

                }, err -> {
                    Log.d(TAG, "通讯异常->" + err.getMessage());
                });
    }

    public void deviceWriteAsyn(DeviceModel deviceModel, List<DeviceAttribute> atts) {
        for (int i = 0; i < atts.size(); i++) {
            deviceWriteAsyn(deviceModel, atts.get(i).getAttrTag(), atts.get(i).getAttrValue());
        }
    }

    public void deviceWriteAsyn(DeviceModel deviceModel, DeviceAttribute attribute) {
        deviceWriteAsyn(deviceModel, attribute.getAttrTag(), attribute.getAttrValue());
    }
}
